package me.ahoukuse.capturedemov1;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {
    private final int CAMERA_REQUEST_CODE = 1;  // 相机请求权限的代码
    private EditText image_width;
    private EditText image_height;
    private EditText server_ip;
    private RadioButton radioButtonTcp;
    private RadioButton radioButtonUDP;
    private EditText fragmentSize;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btPush = (Button) findViewById(R.id.button_push);
        fragmentSize = (EditText) findViewById(R.id.fragment_size);
        fragmentSize.setEnabled(false);
        image_height = (EditText) findViewById(R.id.image_height);
        image_height.setText("350");
        image_width = (EditText) findViewById(R.id.image_width);
        image_width.setText("600");
        radioButtonTcp = (RadioButton) findViewById(R.id.radio_tcp_mode);
        radioButtonUDP = (RadioButton) findViewById(R.id.udp_mode_radio);
        radioButtonUDP.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                fragmentSize.setEnabled(b);
            }
        });
        radioButtonTcp.setChecked(true);
        server_ip = (EditText) findViewById(R.id.server_ip);
        server_ip.setText("192.168.1.111");

        // 判断是否拥有相机使用权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ==
                PackageManager.PERMISSION_DENIED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_REQUEST_CODE);
        }
        final Context that = this;
        btPush.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(that, CameraPreview.class);
                int width = Integer.parseInt(image_width.getText().toString());
                int height = Integer.parseInt(image_height.getText().toString());
                String ip = server_ip.getText().toString();
                intent.putExtra("image_height", height);
                intent.putExtra("image_width", width);
                intent.putExtra("server_ip", ip);
                intent.putExtra("trans_mode", radioButtonTcp.isChecked() ? 0 : 1);
                intent.putExtra("fragment_size", Integer.parseInt(fragmentSize.getText().toString()));
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }

}
